package com.infindo.appcreate.zzyj.controller.mobi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.cxf.common.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.infindo.appcreate.zzyj.entity.Activity;
import com.infindo.appcreate.zzyj.entity.ActivityTalk;
import com.infindo.appcreate.zzyj.entity.Expert;
import com.infindo.appcreate.zzyj.entity.ExpertProject;
import com.infindo.appcreate.zzyj.entity.Infomation;
import com.infindo.appcreate.zzyj.entity.Project;
import com.infindo.appcreate.zzyj.entity.ProjectComment;
import com.infindo.appcreate.zzyj.entity.ProjectSupporter;
import com.infindo.appcreate.zzyj.entity.Service;
import com.infindo.appcreate.zzyj.service.ZzyjSpiderService;
import com.infindo.appcreate.zzyj.util.ACConstant;
import com.infindo.appcreate.zzyj.util.StringUtil;
import com.infindo.framework.common.utils.PaginationSupport;
import com.infindo.framework.spring.mvc.tiles2.Tiles;

@Controller
@RequestMapping("/mobi/")
public class ZzyjSpiderController {
    
	
	@Resource
	private ZzyjSpiderService zzyjSpiderService;
	
	/* -------------  start project --------------*/
	
	/**
	 * get projects list
	 * @param mCode : 11 time; 12 score; 13 expert; 14 public; 15 hot (to do)
	 * @param request
	 * @param model
	 * @return
	 */
	 @RequestMapping(value = "projects/{mCode}", method = RequestMethod.GET)
	 public @ResponseBody Map<String,List<Project>> project(@PathVariable String mCode,HttpServletRequest request, Model model) {
	     
	     Map<String,List<Project>> map = new HashMap<String,List<Project>>();
	     
	     List<Project> proList = null;
	     
	     if(StringUtil.isNotBlank(mCode)){
	         proList =zzyjSpiderService.getProjListByMcode(mCode);
	     }
	     
	     if(proList != null){
	        map.put("projects", proList);
	     }
	     
	     return map;
	 }
	 
	  /**
	     * get projects list by paging
	     * @param mCode : 11 time; 12 score; 13 expert; 14 public; 15 hot (to do)
	     * @param request
	     * @param model
	     * @return
	  */
	 @RequestMapping(value = "projsByPage/{mCode}/{page}", method = RequestMethod.GET)
     public @ResponseBody Map<String,Object> projsByPage(@PathVariable String mCode,@PathVariable String page,HttpServletRequest request, Model model) {
         
	     Map<String,Object> map = new HashMap<String,Object>();
	     
	     if (StringUtils.isEmpty(page)) {
	         page = "1";
	     }  
	     
	     int rpp = ACConstant.PAGE_SIZE_10;//request.getParameter("rpp");
	     Integer startIndex = 0;
	     if (StringUtil.isNotEmpty(page)) {
	         startIndex = (Integer.valueOf(page) - 1) * rpp;
	     }
	     
	     List<Project> proList = zzyjSpiderService.getProjsByPage(mCode,rpp,startIndex);
	     int count = zzyjSpiderService.getObjCount(new Project());
	     
	     if(proList != null){
	         map.put("projects", proList);
	         map.put("count", count+"");
	     }
	     return map;
     }
    
	 /**
	  * project supporters list
	  * @param proCode: project code
	  * @param request
	  * @param model
	  * @param type: 0 expert; 1 public;
	  * @return
	  */
	 @RequestMapping(value = "supporters/{proCode}/{type}", method = RequestMethod.GET)
     public @ResponseBody Map<String,List<ProjectSupporter>> supporters(@PathVariable String proCode,@PathVariable Integer type,HttpServletRequest request, Model model) {
         
         Map<String,List<ProjectSupporter>> map = new HashMap<String,List<ProjectSupporter>>();
         
         List<ProjectSupporter> proSList = null;
         
         if(type == null){
             type = 0;
         }
         
         if(StringUtil.isNotBlank(proCode)){
             proSList =zzyjSpiderService.getProjSupListByCodes(proCode,type);
         }
         
         if(proSList != null){
            map.put("proSupporters", proSList);
         }
         
         return map;
     }
	 
	 @RequestMapping(value = "supptersByPage/{proCode}/{type}/{page}", method = RequestMethod.GET)
     public @ResponseBody Map<String,Object> supportersByPage(@PathVariable String proCode,@PathVariable Integer type,@PathVariable String page,HttpServletRequest request, Model model) {
         
         Map<String,Object> map = new HashMap<String,Object>();
         
         List<ProjectSupporter> proSList = null;
         
         if (StringUtils.isEmpty(page)) {
             page = "1";
         }
         
         if(type == null){
             type = 0;
         }
         
         int rpp = ACConstant.PAGE_SIZE_10;//request.getParameter("rpp");
         Integer startIndex = 0;
         if (StringUtil.isNotEmpty(page)) {
             startIndex = (Integer.valueOf(page) - 1) * rpp;
         }
         
         if(StringUtil.isNotBlank(proCode)){
             proSList =zzyjSpiderService.getProjSupListByCodesPage(proCode,type,rpp,startIndex);
         }
         int count = zzyjSpiderService.getObjCount(new ProjectSupporter());
         
         if(proSList != null){
            map.put("proSupporters", proSList);
         }
         map.put("count", count);
         return map;
         
     }
	 
	 
	 /**
	  * project comments
	  * @param proCode
	  * @param type: 0 expert; 1 public;
	  * @param request
	  * @param model
	  * @return
	  */
     @RequestMapping(value = "comments/{proCode}/{type}", method = RequestMethod.GET)
     public @ResponseBody Map<String,List<ProjectComment>> comments(@PathVariable String proCode,@PathVariable Integer type,HttpServletRequest request, Model model) {
         
         Map<String,List<ProjectComment>> map = new HashMap<String,List<ProjectComment>>();
         
         List<ProjectComment> proCList = null;
         
         if(type == null){
             type = 0;
         }
         
         if(StringUtil.isNotBlank(proCode)){
             proCList =zzyjSpiderService.getProjComtListByCodes(proCode,type);
         }
         
         if(proCList != null){
            map.put("proComments", proCList);
         }
         
         return map;
     }
	 
     /**
      * project comments
      * @param proCode
      * @param type: 0 expert; 1 public;
      * @param request
      * @param model
      * @return
      */
     @RequestMapping(value = "commentsByPage/{proCode}/{type}/{page}", method = RequestMethod.GET)
     public @ResponseBody Map<String,Object> commentsByPage(@PathVariable String proCode,@PathVariable Integer type,@PathVariable String page,HttpServletRequest request, Model model) {
         
         Map<String,Object> map = new HashMap<String,Object>();
         
         List<ProjectComment> proSList = null;
         
         if (StringUtils.isEmpty(page)) {
             page = "1";
         }
         
         if(type == null){
             type = 0;
         }
         
         int rpp = ACConstant.PAGE_SIZE_10;//request.getParameter("rpp");
         Integer startIndex = 0;
         if (StringUtil.isNotEmpty(page)) {
             startIndex = (Integer.valueOf(page) - 1) * rpp;
         }
         
         if(StringUtil.isNotBlank(proCode)){
             proSList =zzyjSpiderService.getCommentsByPage(proCode,type,rpp,startIndex);
         }
         int count = zzyjSpiderService.getObjCount(new ProjectComment());
         
         if(proSList != null){
            map.put("proComments", proSList);
         }
         map.put("count", count);
         return map;
     }
     /* -------------  end project --------------*/
	 
	 
     
     /* -------------  start service  -------------- */
     @RequestMapping(value = "service", method = RequestMethod.GET)
     public @ResponseBody Map<String,Service> service(HttpServletRequest request, Model model) {
         
         Map<String,Service> map = new HashMap<String,Service>();
         
         List<Service> t =zzyjSpiderService.getObjList(Service.class);
         
         if(t.size() > 0){
            map.put("service", t.get(0));
         }
         return map;
     }
     
     /* -------------  end service --------------*/
     
     
     /* -------------  start expert  -------------- */
     @RequestMapping(value = "experts", method = RequestMethod.GET)
     public @ResponseBody Map<String,List<Expert>> expert(HttpServletRequest request, Model model) {
         
         Map<String,List<Expert>> map = new HashMap<String,List<Expert>>();
         
         List<Expert> expertList = null;
         
         expertList =zzyjSpiderService.getExpertList();
         
         if(expertList != null){
            map.put("experts", expertList);
         }
         
         return map;
     }
     
      /**
         * get projects list by paging
         * @param mCode : 11 time; 12 score; 13 expert; 14 public; 15 hot (to do)
         * @param request
         * @param model
         * @return
      */
     @RequestMapping(value = "expertsByPage/{page}", method = RequestMethod.GET)
     public @ResponseBody Map<String,Object> expertsByPage(@PathVariable String page,HttpServletRequest request, Model model) {
         
         Map<String,Object> map = new HashMap<String,Object>();
         
         if (StringUtils.isEmpty(page)) {
             page = "1";
         }  
         
         int rpp = ACConstant.PAGE_SIZE_10;//request.getParameter("rpp");
         Integer startIndex = 0;
         if (StringUtil.isNotEmpty(page)) {
             startIndex = (Integer.valueOf(page) - 1) * rpp;
         }
         
         List<Expert> expertList = zzyjSpiderService.getExpertsByPage(rpp,startIndex);
         int count = zzyjSpiderService.getObjCount(new Expert());
         
         if(expertList != null){
             map.put("experts", expertList);
             map.put("count", count+"");
         }
         return map;
     }
     
     /**
      * get expert projects
      * @param type: 0 support; 1 arise
      * @param request
      * @param model
      * @return
      */
     @RequestMapping(value = "expertProjs/{expertCode}/{type}", method = RequestMethod.GET)
     public @ResponseBody Map<String,List<Project>> expertProjs(@PathVariable String expertCode,@PathVariable Integer type,HttpServletRequest request, Model model) {
         
         Map<String,List<Project>> map = new HashMap<String,List<Project>>();
         
         List<Project> expProjsList = null;
         
         expProjsList =zzyjSpiderService.getExpertProjsList(expertCode,type);
         
         if(expProjsList != null){
            map.put("expertProjs", expProjsList);
         }
         
         return map;
     }
     
     /**
      * comments by expert
      * @param expertCode
      * @param request
      * @param model
      * @return
      */
     @RequestMapping(value = "expertComts/{expertCode}", method = RequestMethod.GET)
     public @ResponseBody Map<String,List<Object>> expertComts(@PathVariable String expertCode,HttpServletRequest request, Model model) {
         
         Map<String,List<Object>> map = new HashMap<String,List<Object>>();
         
         List<Object> commentList = null;
         
         commentList =zzyjSpiderService.getExpertComtsList(expertCode);
         
         if(commentList != null){
            map.put("expertComts", commentList);
         }
         
         return map;
     }
     
     
     /* -------------  end expert  -------------- */
     
     
     /* ------------- activity start -------------*/
     
     @RequestMapping(value = "activitys", method = RequestMethod.GET)
	 public @ResponseBody Map<String,List<Activity>> activitys(HttpServletRequest request, Model model) {
	     
	     Map<String,List<Activity>> map = new HashMap<String,List<Activity>>();
	     
	     List<Activity> actsList = null;
	     
	     actsList = zzyjSpiderService.getActivityList();
	     
	     if(actsList != null){
	        map.put("projects", actsList);
	     }
	     
	     return map;
	 }
     
     @RequestMapping(value = "activitysByPage/{page}", method = RequestMethod.GET)
   	 public @ResponseBody Map<String,Object> activitysByPage(@PathVariable String page, HttpServletRequest request, Model model) {
   	     
    	 Map<String,Object> map = new HashMap<String,Object>();
         
         if (StringUtils.isEmpty(page)) {
             page = "1";
         }  
         
         int rpp = ACConstant.PAGE_SIZE_10;//request.getParameter("rpp");
         Integer startIndex = 0;
         if (StringUtil.isNotEmpty(page)) {
             startIndex = (Integer.valueOf(page) - 1) * rpp;
         }
         
         List<Activity> actList = zzyjSpiderService.getExpertsByPage(rpp,startIndex);
         int count = zzyjSpiderService.getObjCount(new Activity());
         
         if(actList != null){
             map.put("activitys", actList);
             map.put("count", count+"");
         }
         return map;
   	 }
     
     @RequestMapping(value = "activityTalk/{actCode}", method = RequestMethod.GET)
	 public @ResponseBody Map<String,List<ActivityTalk>> activityTalk(@PathVariable String actCode,HttpServletRequest request, Model model) {
	     
	     Map<String,List<ActivityTalk>> map = new HashMap<String,List<ActivityTalk>>();
	     
	     List<ActivityTalk> actsList = null;
	     
	     actsList = zzyjSpiderService.getActivityTalks(actCode);
	     
	     if(actsList != null){
	        map.put("activityTalks", actsList);
	     }
	     
	     return map;
	 }
     
     /* ------------- activity end ---------------*/
     
     /* ------------- infomation start -----------*/
     @RequestMapping(value = "infos", method = RequestMethod.GET)
	 public @ResponseBody Map<String,List<Infomation>> infos(HttpServletRequest request, Model model) {
	     
	     Map<String,List<Infomation>> map = new HashMap<String,List<Infomation>>();
	     
	     List<Infomation> infoList = null;
	     
	     infoList = zzyjSpiderService.getInfomationList();
	     
	     if(infoList != null){
	        map.put("infomations", infoList);
	     }
	     
	     return map;
	 }
     
     @RequestMapping(value = "infosByPage/{page}", method = RequestMethod.GET)
   	 public @ResponseBody Map<String,Object> infosByPage(@PathVariable String page, HttpServletRequest request, Model model) {
   	     
    	 Map<String,Object> map = new HashMap<String,Object>();
         
         if (StringUtils.isEmpty(page)) {
             page = "1";
         }  
         
         int rpp = ACConstant.PAGE_SIZE_10;//request.getParameter("rpp");
         Integer startIndex = 0;
         if (StringUtil.isNotEmpty(page)) {
             startIndex = (Integer.valueOf(page) - 1) * rpp;
         }
         
         List<Infomation> actList = zzyjSpiderService.getInfomatinsByPage(rpp,startIndex);
         int count = zzyjSpiderService.getObjCount(new Infomation());
         
         if(actList != null){
             map.put("infomations", actList);
             map.put("count", count+"");
         }
         return map;
   	 }
     /* ------------- infomation end -----------*/
     
     
     
     
     
     
     
     
     
	 
	 
	 
	 
	  
	@RequestMapping(value = "content")
	public @ResponseBody String content(HttpServletRequest request, Model model) {

	    //JSONObject json = JSONObject.fromObject(dataMap);
	    
	    //String prettyJsonString = jsonFormatter(json.toString());
	    
	    //System.out.println( prettyJsonString);
	    
	    return "";
	    
	}
	public static String jsonFormatter(String uglyJSONString){
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(uglyJSONString);
        String prettyJsonString = gson.toJson(je);
        return prettyJsonString;
    }

}

 
